package com.win.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;

@Configuration
@EnableSwagger2
//@Profile({"dev", "test"})
public class SwaggerConfig {

    private static final String BASE_PACKAGE = "com.win.oauth.controller";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("base")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(Arrays.asList(buildHeader()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("oauth-swagger2")    //标题
                .description("Restful-API-Doc")    //描述
                .termsOfServiceUrl("") //这里配置的是服务网站
                .contact(new Contact("youlian.fei", "", "feiyoulian.@gmail.com")) // 三个参数依次是姓名，个人网站，邮箱
                .version("1.0") //版本
                .build();
    }


    private Parameter buildHeader() {
        //在配置好的配置类中增加此段代码即可
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("accesstoken").description("登录校验")//name表示名称，description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).defaultValue("token").build();//required表示是否必填，defaultvalue表示默认值

        return ticketPar.build();
    }

}
